<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="umsroot.css">
<TITLE>
Declarative Suspension: Delay Clauses
</TITLE>
</HEAD>
<BODY >
<A HREF="umsroot108.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="umsroot104.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="umsroot110.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc237">17.5</A>&nbsp;&nbsp;Declarative Suspension: Delay Clauses</H2>

For delaying calls to user-defined Prolog predicates, ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP>
 provides several alternatives, the first being <I>delay clauses</I>.
<A NAME="@default960"></A>
Delay clauses are a declarative means (they are in fact meta-clauses)
to specify the conditions under which the predicate should delay.
The semantics of delay clauses is thus cleaner than many alternative
approaches to delay primitives.<BR>
<BR>
A delay clause is very similar to a normal Prolog clause. It has the form
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
delay &lt;Head&gt; if &lt;Body&gt;.
</PRE></BLOCKQUOTE>
A predicate may have one or more delay clauses.
They have to be textually <B>before</B> and <B>consecutive</B>
with the normal clauses of the predicate they belong to.
The simplest example for a delay clause is one that checks if a variable
is instantiated:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
delay report_binding(X) if var(X).
report_binding(X) :-
        printf("Variable has been bound to %w\n", [X]).
</PRE></BLOCKQUOTE>
The operational semantics of the delay clauses is as follows:
when a procedure with delay clauses is called, then the delay
clauses are executed before executing the procedure itself.
If one of the delay clauses succeeds, the call is suspended,
otherwise they are all tried in sequence and,
if all delay clauses fail, the procedure is executed as usual.<BR>
<BR>
The mechanism of executing a delay clause is similar to normal Prolog
clauses with two exceptions:
<UL CLASS="itemize"><LI CLASS="li-itemize">
the unification of the goal with the delay clause head is not the usual
Prolog unification, but rather unidirectional pattern matching
(see also section <A HREF="umsroot026.html#matching">5.5</A>).
<A NAME="@default961"></A>
<A NAME="@default962"></A>
This means that the variables in the call cannot be bound
by the matching, if such a binding would be necessary to
perform the unification, it will fail instead.
E.g. the head of the delay clause
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
delay p(a, X) if var(X).
</PRE></BLOCKQUOTE>
does not match the goal <B>p(A, b)</B> but it matches the goal <B>p(a, b)</B>.<BR>
<BR>
<LI CLASS="li-itemize">the delay clauses are deterministic, they leave no choice points.
If one delay clause succeeds, the call is delayed and the following delay
clauses are not executed.
As soon as the call is resumed, all delay clauses that may succeed
are re-executed.
</UL>
The reason for using pattern matching instead of unification
is to avoid a possible mixing of meta-level control with the
object level, similarly to [<A HREF="umsroot159.html#dincbas84"><CITE>3</CITE></A>].<BR>
<BR>
The form of the head of a delay clause is not restricted.
For the body, the following conditions hold:
<UL CLASS="itemize"><LI CLASS="li-itemize">
the body subgoals must not bind any variable in the call and they
must not delay themselves.
The system does not verify these conditions currently.<BR>
<BR>
<LI CLASS="li-itemize">it should contain at least one of the following subgoals:
<UL CLASS="itemize"><LI CLASS="li-itemize">
<A HREF="../bips/kernel/typetest/var-1.html"><B>var/1</B></A><A NAME="@default963"></A>
<LI CLASS="li-itemize"><A HREF="../bips/kernel/typetest/nonground-1.html"><B>nonground/1</B></A><A NAME="@default964"></A>
<LI CLASS="li-itemize"><B>nonground/2</B> (see <A HREF="../bips/kernel/typetest/nonground-3.html"><B>nonground/3</B></A><A NAME="@default965"></A>)
<LI CLASS="li-itemize"><B>\==/2</B>
<A NAME="@default966"></A>
</UL>
If this is not the case, then the predicate may delay without being linked
to a variable, so it delays forever and cannot be woken again.
Experience shows that the above four primitives suffice to express most
usual conditions.
</UL>

<H4 CLASS="subsubsection">More Examples</H4>
<UL CLASS="itemize"><LI CLASS="li-itemize">
A predicate that checks if its argument is a proper list of integers.
The delay conditions specify that the predicate should delay if the list
is not terminated or if it contains variable elements.
This makes sure that it will never generate list elements, but only
acts as a test:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
delay integer_list(L) if var(L).
delay integer_list([X|_]) if var(X).
integer_list([]).
integer_list([X|T]) :- integer(X), integer_list(T).  
</PRE></BLOCKQUOTE><BR>
<BR>
<LI CLASS="li-itemize">Delay if the first two arguments are identical and the third is a variable:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
delay p(X, X, Y) if var(Y).
</PRE></BLOCKQUOTE><BR>
<BR>
<LI CLASS="li-itemize">Delay if the argument is a structure whose first subterm is not ground:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
delay p(X) if compound(X), arg(1, X, Y), nonground(Y).
</PRE></BLOCKQUOTE><BR>
<BR>
<LI CLASS="li-itemize">Delay if the argument term contains 2 or more variables:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
delay p(X) if nonground(2, X).
</PRE></BLOCKQUOTE><BR>
<BR>
<LI CLASS="li-itemize">The
<B>\==/2</B>
<A NAME="@default967"></A>
predicate as a delaying condition is useful mainly
in predicates like X + Y = Z which need not be delayed if X == Z.
Y can be directly bound to 0, provided that X is later bound to a number
(or it is not bound at all)
The condition <B>X \== Y</B> makes sense
only if X or Y are nonground: a delay clause
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
delay p(X, Y) if X \== Y.
</PRE></BLOCKQUOTE>
executed with the call ?- p(a, b) of course succeeds and the call delays
forever, since no variable binding can wake it.
</UL>
<B>CAUTION</B>: It may happen that the symbol <B>:-</B> is erroneously
used instead of <B>if</B> in the delay clause. To indicate this error,
the compiler complains about redefinition of the built-in predicate
<B>delay/1</B>.<BR>
<BR>
<HR>
<A HREF="umsroot108.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="umsroot104.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="umsroot110.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
